package com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.DO;

import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.dto.SentinelParamFlowRuleDTO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import java.io.Serializable;

/**
 * xxx
 *
 * @author BugFree Jin
 * @version 1.0
 * @since 2022/3/2 15:42
 */
@Data
@Table(value = "sentinel_param_flow_rule")
public class SentinelParamFLowRuleDO extends BaseDO {

    private static final long serialVersionUID = 920803190777284688L;
    @Id
    private long id;

    @Column("param_flow_rule")
    private String paramFlowRule;

    public SentinelParamFlowRuleDTO getSentinelParamFlowRuleDTO() {
        SentinelParamFlowRuleDTO sentinelParamFlowRuleDTO = new SentinelParamFlowRuleDTO();
        try {
            ParamFlowRule pfr = new ObjectMapper().readValue(this.paramFlowRule, ParamFlowRule.class);
            BeanUtils.copyProperties(pfr, sentinelParamFlowRuleDTO);
            BeanUtils.copyProperties(this, sentinelParamFlowRuleDTO, "paramFlowRule");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return sentinelParamFlowRuleDTO;
    }

    @Data
    public static class ParamFlowRule implements Serializable {

        private Integer burstCount;
        // todo: ParamFlowClusterConfig 对象
        private String clusterConfig;
        private Boolean clusterMode;
        private Integer controlBehavior;
        private Double count;
        private Long durationInSec;
        private Integer grade;
        private Integer maxQueueingTimeMs;
        // todo: paramFlowItemList 为 jsonArray
        private String paramFlowItemList;
        private Integer paramIdx;
        private String limitApp;
        private String resource;
        private static final long serialVersionUID = -7810887698638916796L;
    }

}
